package com.jkoss.wine.statistics.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.statistics.service.IMemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import com.jkoss.base.controller.BaseController;

/**
 *  前端控制器
 * 
 * @Author bg2it
 * @Version 1.0, 2019-08-21
 * @See
 * @Since com.jkoss.wine.statistics.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/statistics/member")
public class MemberController extends BaseController {


	@Autowired
	private IMemberService iMemberService;
	
	@RequestMapping("/list")
	@RequiresPermissions("/statistics/member/list")
	public String list(String shop,String memberName,DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
        dwzPageBean.getCountResultMap().put("shop", shop);
        dwzPageBean.getCountResultMap().put("memberName", memberName);
	    Wrapper wrapper = Condition.create().ne("m.del",1).like("m.member_name",memberName).like("s.shop_name",shop);
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			//默认排序字段
			wrapper.orderBy("m.add_time", false);
		}
		Page resultPage = iMemberService.selectVoByPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "statistics/member/list";
	}

	/*@RequestMapping("/toInsert")
	@RequiresPermissions("/statistics/member/toInsert")
	public String toInsert(HttpServletRequest request, HttpServletResponse response) {
		return "statistics/member/edit";
	}

	@RequestMapping("/insert")	
	@RequiresPermissions("/statistics/member/toInsert")
	@ResponseBody
	public Object insert(@Valid Member member, HttpServletRequest request, HttpServletResponse response) {
		if (iMemberService.insert(member)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	@RequestMapping("/toUpdate")
	@RequiresPermissions("/statistics/member/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", iMemberService.selectById(id));
		return "statistics/member/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/statistics/member/toUpdate")
	@ResponseBody
	public Object update(@Valid Member member, HttpServletRequest request, HttpServletResponse response) {
		if (iMemberService.updateById(member)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	@RequestMapping("/delete")
	@RequiresPermissions("/statistics/member/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		if (iMemberService.deleteBatchIds(Arrays.asList(id))) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}*/
	

}

